package com.tbit.main.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.core.AccountUserWorkConfigDao;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.AccountUserBound;
import com.tbit.main.pojo.AccountUserWorkConfig;
import com.tbit.main.service.AccountUserService;
import com.tbit.main.service.AccountUserWorkConfigService;
import com.tbit.main.service.GeoreGionService;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author LMY
 * @create 2021-08-23 11:51
 * 运维人员配置
 */
@Service("accountUserWorkConfigService")
public class AccountUserWorkConfigServiceImpl implements AccountUserWorkConfigService {

    @Autowired
    private AccountUserWorkConfigDao accountUserWorkConfigDao;
    @Autowired
    private GeoreGionService georeGionService;
    @Autowired
    private AccountUserService accountUserService;

    @Override
    public List<AccountUserWorkConfig> getAccountId(Integer accountId, List<Integer> regionId, Integer accountUserId) {
        Map<String, Object> params = new HashMap<>();
        if (CollUtil.isEmpty(regionId)) {
            params.put("exist", "'exist'");
            regionId = georeGionService.getByAccountUserId(accountUserId);
        } else {
            params.put("exist", null);
        }
        params.put("regionId", StringUtil.getListString(regionId));
        params.put(DaoParamConstant.accountId, accountId);

        List<AccountUserWorkConfig> accountUserWorkConfigList = accountUserWorkConfigDao.getAccountId(params);

        if (accountUserWorkConfigList == null) {
            List<AccountUser> accountUserMainMans = new LinkedList<>();
            if (CollUtil.isEmpty(regionId)) {
                accountUserMainMans = accountUserService.getByAccountUserType(new AccountUserBound(accountId, 1));
            } else {
                accountUserMainMans = accountUserService.getRegionIdsUser(regionId, 1);
            }

            for (AccountUser au : accountUserMainMans) {
                AccountUserWorkConfig accountUserWorkConfig = new AccountUserWorkConfig();
                accountUserWorkConfig.setAccountUserId(au.getAccountUserId());
                accountUserWorkConfig.setAccountUserName(au.getName());
                accountUserWorkConfigList.add(accountUserWorkConfig);
            }
        }

        return accountUserWorkConfigList;
    }

    @Override
    public void del(Integer accountUserId) {
        accountUserWorkConfigDao.del(accountUserId);
    }

    @Override
    public void addList(List<AccountUserWorkConfig> accountUserWorkConfigList) {
        accountUserWorkConfigDao.addList(accountUserWorkConfigList);
    }

    @Override
    public List<AccountUserWorkConfig> getAccountUserId(Integer accountUserId) {
        return accountUserWorkConfigDao.getAccountUserId(accountUserId);
    }

    @Override
    public List<AccountUserWorkConfig> getByAccountIds(List<Integer> accountIds, Integer workType) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountIds, StringUtil.getListString(accountIds));
        map.put("workType", workType);
        map.put("time", DateTimeUtil.getNowDayTime());
        return accountUserWorkConfigDao.getByAccountIds(map);
    }

    @Override
    public List<AccountUserWorkConfig> getAll() {
        return accountUserWorkConfigDao.getAll();
    }

    @Override
    public List<AccountUserWorkConfig> getByWorkNumber(Integer workNumber) {
        return accountUserWorkConfigDao.getByWorkNumber(workNumber);
    }

    @Override
    public List<AccountUserWorkConfig> getAccountUserIds(Collection<Integer> accountUserIds) {
        if (CollUtil.isEmpty(accountUserIds)) {
            return Collections.emptyList();
        }
       return accountUserWorkConfigDao.selectAccountUserIds(StringUtil.getListString(accountUserIds));
    }
}
